<template>
  <div class="app-container calendar-list-container">
    <!-- filter 条件查询 -->
    <div class="filter-container">
      <el-input clearable @keyup.enter.native="handleFilter" v-model="listQuery.parnerPhone" style="width: 200px;" class="filter-item" :placeholder="$t('placeholder.userPhone')" >
      </el-input>
      <el-input clearable @keyup.enter.native="handleFilter" v-model="listQuery.parnerName" style="width: 200px;" class="filter-item" :placeholder="$t('placeholder.nickName')" >
      </el-input>
      <el-select v-model="listQuery.status" :placeholder="$t('placeholder.status')" clearable class="filter-item" style="width: 250px;">
        <el-option :label="$t('agent.Applying')" value="1"></el-option>
        <el-option :label="$t('agent.passed')" value="2"></el-option>
        <el-option :label="$t('agent.reject')" value="3"></el-option>
        <el-option :label="$t('agent.Interview')" value="4"></el-option>
      </el-select>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">{{$t('common.Search')}}</el-button>
    </div>
    <!-- table 表格 -->
    <el-table  :data="list" v-loading="loading.list" :element-loading-text="$t('common.Loading')" border fit highlight-current-row
      style="width: 100%">      
      <el-table-column  align="center" :label="$t('agent.Number')" prop='index'/>
      <el-table-column  align="center" :label="$t('common.Phone_number')" prop='parnerPhone'/>
      <el-table-column  align="center" :label="$t('common.Email')" prop='email'/>
      <el-table-column  align="center" :label="$t('common.Store_Name')" prop='shopName'/>
      <el-table-column  align="center" :label="$t('home.Address')" prop='address'/>
      <el-table-column  align="center" :label="$t('home.Introduction')" prop='intro'/>
      <el-table-column  align="center" :label="$t('financial.Application_time')" prop='ctime'>
        <template slot-scope="scope">
          {{scope.row.ctime | parseTime}}
        </template>
      </el-table-column>
      <el-table-column  align="center" :label="$t('agent.status')" prop='status'>
        <template slot-scope="scope">
          {{scope.row.status == 1 ? $t('agent.Applying'):scope.row.status == 2?$t('agent.passed'):scope.row.status == 3?$t('agent.reject'): $t('agent.Interview')}}
        </template>
      </el-table-column>
      <el-table-column  align="center" :label="$t('agent.reject_reason')" prop='reason'/>
      <el-table-column align="center"  :label="$t('table.Operate')"  class-name="small-padding fixed-width">
        <template slot-scope="scope"> 
            <router-link :to="{name:'join_detail', query: {id: scope.row.id}}">
              <el-button type="text">{{$t('agent.detail')}}</el-button>
            </router-link>
            <!-- <router-link :to="{name:'hospital_detail', query: {hospitalId: scope.row.hospitalId,roleLevel:'1'}}"> -->
              <el-button type="text"  v-if="scope.row.status == 1" @click="audit(scope.row.id,4)">{{$t('agent.Interview')}}</el-button>
            <!-- </router-link> -->
            <el-button type="primary"  v-if="scope.row.status == 1 || scope.row.status == 4" @click="audit(scope.row.id,2)">{{$t('agent.passed')}}</el-button>
            <el-button type="danger"  v-if="scope.row.status == 1 || scope.row.status == 4" @click="audit(scope.row.id,3)">{{$t('agent.reject')}}</el-button>
            <!-- <el-button type="danger" size="mini" v-if="scope.row.isPlatform == 1" @click="delHospital(scope.row.hospitalId)">删除</el-button>        -->
        </template>
      </el-table-column>
    </el-table>
    <!-- pagination 分页 -->
    <div class="pagination-container">
      <el-pagination 
      background 
      @size-change="handleSizeChange" 
      @current-change="handleCurrentChange" 
      :current-page="listQuery.pageNo" 
      :page-sizes="[10, 20, 30, 40]" 
      :page-size="listQuery.pageSize" 
      layout="total, sizes, prev, pager, next, jumper" 
      :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="$t('agent.reject')" :visible.sync="dialog.add" @close='_tempform = null;addQuery.reason = "" '>
        <el-form  :model="addQuery" :rules='addRules' ref="addWard" label-position="right" label-width="120px" >  
            <el-form-item :label="$t('agent.reject_reason')">
                <el-input v-model="addQuery.reason" type="textarea" :placeholder="$t('agent.reject_reason')"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button  @click="dialog.add = false">{{$t('common.Cancel')}}</el-button>   
            <el-button type="primary" :loading="loading.add" @click="update">{{$t('common.Confirm')}}</el-button>     
        </div>
    </el-dialog> 
  </div>
</template>
<script>
import { getParnerListConditionByPage, updateParnerStatus } from '@/api/agent'
export default {
  name: 'agent_join',
  data() {
    return {
      labelPosition: 'right',
      total: 0,
      list: null,
      loading: {
        list: false
      },
      dialog: {
        charge: false,
        add: false
      },
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        parnerPhone:'',
        parnerName: '',
        status: ''       
      },
      addRules: [],
      addQuery:{
        reason: ''
      },
      rules: {}      
    }
  },
  created() {
    this.getList()
  },
  methods: {
    //获取加盟申请列表
    async getList() {
      this.loading.list = true
      const { data } = await getParnerListConditionByPage(this.listQuery)
      this.list = data.records.map((item, index) => ({index: index + 1, ...item}))
      this.total = data.total
      this.loading.list = false
    },
    //审核
    //如果==3时
    async update(id,status) {
      try{
          const { data } = await updateParnerStatus({reason: this.addQuery.reason, ...this._tempform})
            this.$message({
              type: 'success',
              message: `驳回成功`
            })
            this.dialog.add = false
            this.getList()
        } catch(error) {

      }
    },
    async audit(id,status) {
      let type = status == 2?'通过': status == 3?'驳回':'面谈'
      if(status == 3) {
        this.dialog.add = true
        this._tempform = {
          id,
          status
        }
        // this.update(id,status)
      }else {
        this.$confirm(`是否${type}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
          }).then(async () => {
            try{
              const { data } = await updateParnerStatus({id,status})
                this.$message({
                  type: 'success',
                  message: `${type}成功`
                })
                this.getList()
            } catch(error) {

            }
          })
        }
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val
      this.getList()
    }
  }
}
</script>
